package org.project.d.network.online;

import org.apache.commons.lang3.ArrayUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RKeys;
import org.redisson.api.RedissonClient;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author lin
 * @version V1.0
 * @description
 * @date 2020/10/16 3:55 下午
 */
public class RedisRemoteOnlineManager implements RemoteOnlineManager {
    private static final long keepliveTime=5L;
    private final RedissonClient redissonClient;

    public RedisRemoteOnlineManager(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public boolean online(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)) {
            RBucket<OnlineInfo> bucket = redissonClient.getBucket(getOnlineUserListKey(onlineInfo));
            bucket.set(onlineInfo,keepliveTime, TimeUnit.MINUTES);
            return true;
        }
        return false;
    }

    @Override
    public boolean offline(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)) {
            RBucket<OnlineInfo> bucket = redissonClient.getBucket(getOnlineUserListKey(onlineInfo));
            bucket.delete();
            return true;
        }
        return false;
    }

    private String getOnlineUserListKey(OnlineInfo onlineInfo) {
        return CacheKeys.getOnlineUserListKey(onlineInfo.getUserId().toString());
    }

    @Override
    public boolean kickline(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)) {
            RBucket<OnlineInfo> bucket = redissonClient.getBucket(getOnlineUserListKey(onlineInfo));
            bucket.delete();
            return true;
        }
        return false;
    }

    @Override
    public OnlineInfo selectOnlineInfo(String id) {
        RBucket<OnlineInfo> bucket = redissonClient.getBucket(CacheKeys.getOnlineUserListKey(id));
        return bucket.get();
    }

    @Override
    public boolean update(OnlineInfo onlineInfo) {
        if (checkId(onlineInfo)){
            if (onlineInfo.getOnlineState()==OnlineState.OFF_LINE.getState()){
                return offline(onlineInfo);
            }
            if (onlineInfo.getOnlineState()==OnlineState.ON_LINE.getState()){
                return online(onlineInfo);
            }
            RBucket<OnlineInfo> bucket = redissonClient.getBucket(getOnlineUserListKey(onlineInfo));
            bucket.set(onlineInfo,keepliveTime, TimeUnit.MINUTES);
            return true;
        }
        return false;
    }

    @Override
    public void keepLive(String id) {
        RKeys keys = redissonClient.getKeys();
        keys.expire(CacheKeys.getOnlineUserListKey(id),keepliveTime, TimeUnit.MINUTES);
    }

    @Override
    public void offlineAll() {

    }

    private boolean checkId(OnlineInfo onlineInfo) {
        return onlineInfo!=null&&onlineInfo.getUserId()!=null;
    }

    @Override
    public void offline(Set<String> cacheKeys) {
        RKeys keys = redissonClient.getKeys();
        String[] strings = ArrayUtils.toStringArray(cacheKeys.toArray());
        if (strings.length>0){
            keys.delete(strings);
        }
    }
}
